package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 分页查询（带条件）
     *
     * @param studentQueryParam
     */
    @Override
    public PageBean findAll(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        Page<Student> studentList= studentMapper.findAll(studentQueryParam);

        return new PageBean(studentList.getTotal(),studentList.getResult());
    }

    /**
     * 根据id查询学员
     */
    @Override
    public Student getById(Integer id) {

        return  studentMapper.getById(id);
    }

    /**
     * 批量删除学员
     * @param ids 要被删除学员的id
     */
    @Override
    public void deleteBatch(List<Integer> ids) {
        studentMapper.deleteBatch(ids);

    }

    /**
     * 添加学生
     *
     * @param student 要添加的学生的信息
     */
    @Override
    public void addStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }

    /**
     * 修改学生信息
     *
     * @param student 封装要修改学生的信息
     */
    @Override
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.updateStudent(student);
    }

    /**
     * 违纪处理
     *
     * @param id    违纪的学员id
     * @param score 扣除的分数
     */
    @Override
    public void violation(Integer id, Integer score) {
        studentMapper.violation(id,score);
    }


}
